import {
  Module,
  NestModule,
  MiddlewareConsumer,
  RequestMethod,
} from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
// 中间件
import {
  CheckParamIsEmptyMiddleware,
  CheckPhoneIsExistsMiddleware,
} from './middlewares/create.middleware';
import loginApiMiddleware from './middlewares/login.middleware';
import batchDelApiMiddleware from './middlewares/batchDel.middleware';
import updateApiMiddleware from './middlewares/update.middleware';
import batchQueryByIDsApiMiddleware from './middlewares/batchQueryByIDs.middleware';
import updateUserPasswordApiMiddleware from './middlewares/updateUserPassword.middleware';
import baseQueryApiMiddleware from './middlewares/baseQuery.middleware';
@Module({
  controllers: [UserController],
  providers: [UserService],
})
// export class UserModule {}
export class UserModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // 拦截这个控制器的接口
    // consumer.apply(LoggerMiddleware).forRoutes('user');
    // 拦截这个控制器下的 指定 格式的接口

    // 拦截所有的
    // consumer.apply(LoggerMiddleware).forRoutes(UserController);

    // 1、注册接口
    consumer
      .apply(CheckParamIsEmptyMiddleware, CheckPhoneIsExistsMiddleware)
      .forRoutes({
        method: RequestMethod.POST,
        path: '/user/create',
      });

    // 2、登录接口
    consumer
      .apply(
        loginApiMiddleware.CheckParamIsEmptyMiddleware,
        loginApiMiddleware.CheckPhoneIsExistMiddleware,
        loginApiMiddleware.CheckPasswordMiddleware,
      )
      .forRoutes({
        // consumer.apply(loginApiMiddleware.CheckParamIsEmptyMiddleware).forRoutes({
        method: RequestMethod.POST,
        path: '/user/login',
      });
    // 3、删除接口
    consumer
      .apply(
        batchDelApiMiddleware.CheckParamIsEmptyMiddleware,
        batchDelApiMiddleware.CheckIDsIsExistMiddleware,
      )
      .forRoutes({
        method: RequestMethod.DELETE,
        path: '/user/batchDel',
      });

    // 4、更新接口

    consumer
      .apply(
        updateApiMiddleware.CheckParamIsEmptyMiddleware,
        updateApiMiddleware.CheckIDIsExistMiddleware,
      )
      .forRoutes({
        method: RequestMethod.POST,
        path: '/user/update',
      });

    // 5、根据用户ID，批量查询接口
    consumer
      .apply(
        batchQueryByIDsApiMiddleware.CheckParamIsEmptyMiddleware,
        batchQueryByIDsApiMiddleware.CheckIDsIsExistMiddleware,
      )
      .forRoutes({
        method: RequestMethod.POST,
        path: '/user/batchQueryByIDs',
      });

    // 6、更新密码
    consumer
      .apply(
        updateUserPasswordApiMiddleware.CheckParamIsEmptyMiddleware,
        updateUserPasswordApiMiddleware.CheckIDIsExistMiddleware,
      )
      .forRoutes({
        method: RequestMethod.POST,
        path: '/user/updateUserPassword',
      });

    // 通用查询接口
    consumer
      .apply(baseQueryApiMiddleware.CheckParamIsEmptyMiddleware)
      .forRoutes({
        method: RequestMethod.POST,
        path: '/user/baseQuery',
      });
  }
}
